package red.stu.wechat.factory.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("wechatLoginSign")
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class WeChatLoginSign {
	
	@Value("${loginkey}")
	private String loginKey;
	
	/**
	 * 获取签名
	 * @param sign
	 * @return
	 */
	public String sign() {
		return MD5Util.md5(String.valueOf(System.currentTimeMillis()).substring(0, 6) + loginKey);
	}
	
	/**
	 * 校验数据
	 * @param sign
	 * @param datas
	 * @return
	 */
	public boolean check(String checkSign, String ... datas) {
		if (checkSign == null)
			return false;
		String loginSign = this.sign();
		StringBuffer dataSign = new StringBuffer(loginSign);
		if (datas != null && datas.length > 0) {
			for (int i = 0; i < datas.length; i++) {
				String dataMd5 = MD5Util.md5(String.valueOf(i) + datas[i]);
				dataSign.append(dataMd5);
			}
		}
		return MD5Util.md5(dataSign.toString()).equals(checkSign);
	}
	
}
